/***************************************************
> Copyright (C) 2024 ==QWZL== All rights reserved.
> File Name: log_staff.c
> Author:nxq
> Mail:nxq949698473@163.com 
> Created Time: 2024年07月24日 星期三 18时14分41秒
***************************************************/

#include "WPH_server.h"
#include "log_staff.h"
#include "sql.h"
#include "sys_log.h"

//员工登录
void staffLog(int sock,char*buf)
{
    int ret = 0;
    int value = 0;

    struct staffLog info = employee(buf);//员工登录解包
    struct sta *head;
    head = (struct sta *)malloc(sizeof(struct sta));
    memset(head,0,sizeof(struct sta));

    head = readInfo();//读取员工表中信息
    struct sta *sta = head->next;
    while(sta != NULL)
    {
        LOG_D("info.account=%d ,sta->account = %d,info.cipher= %d,sta->cipher = %d\n",info.account,sta->account,info.cipher,sta->cipher);
        if(info.account == sta->account && info.cipher == sta->cipher)
        {
            value = 1;//成功
            LOG_D("value1 = %d\n",value);
            break;
        }
        else
        {
            value = 0;
            LOG_D("value2 = %d\n",value);
        }
        sta = sta->next;
    }
    ret = send(sock,&value,4,0);
    if(ret == -1)
    {
        LOG_E("sendto"); 
        exit(-1);
    }
    close(sock);

    struct sta*temp = NULL;
    //释放 
    while(head != NULL)
    {
        temp = head;
        free(temp);
        temp = NULL;
        head = head->next;
    }
}

//员工登录解包
struct staffLog employee(char *buf)
{
    struct staffLog info;
    memset(&info,0,sizeof(info));
    char type[30] = {0};

    //定义cJSON指针
    cJSON *json = NULL;
    //将字符串转为json格式对象
    json = cJSON_Parse(buf);

    cJSON *node = NULL;
    //由键获取值
    node = cJSON_GetObjectItem(json,"type");
    if(node == NULL)
    {
        LOG_E("can't find type"); 
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(type,node->valuestring);
    }

    node = cJSON_GetObjectItem(json,"account");
    if(node == NULL)
    {
        LOG_E("can't find account"); 
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.account = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"cipher");
    if(node == NULL)
    {
        LOG_E("can't find cipher"); 
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.cipher = node->valueint;
    }

    cJSON_Delete(json);	
    return info;
}
